package com.pan.utils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public enum ShortcutKeyConfig {
    INSTANCE;
    private static final String CONFIG_NAME = "ShortcutKeysConfig.properties";

    private Map<String,String> propertiesMap;{
        Properties defaultConfigProperties;
        Properties userConfigProperties;
        defaultConfigProperties = new Properties();
        File userConfigFile = new File("src/main/resources/"+CONFIG_NAME);
        if (userConfigFile.exists()){
            userConfigProperties = new Properties();
            try {
                userConfigProperties.load(new FileReader(userConfigFile));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
           propertiesMap = userConfigProperties.entrySet().stream()
                    .collect(HashMap::new, (map, kv) -> map.put((String) kv.getKey(), (String) kv.getValue()),
                            HashMap::putAll);
        }

        InputStream configInputStream = ShortcutKeyConfig.class.getClassLoader()
                .getResourceAsStream(CONFIG_NAME);
        try {
            defaultConfigProperties.load(configInputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        defaultConfigProperties.entrySet()
                .stream().filter(kv->!propertiesMap.containsKey(kv.getKey()))
                .forEach((kv)-> propertiesMap.put((String) kv.getKey(), (String) kv.getValue()));

    }

    public String getValue(String key){
        return propertiesMap.get(key);
    }

    public String getMarkMouseKey(){
        return getValue("mark.mouse.key");
    }

    public String getDistanceKey(){
        return getValue("mark.distance.key");
    }

    public String getOriginKey() {
        return getValue("origin.reset.key");
    }
    public String getQuitKey(){
        return getValue("app.quit.key");
    }

}
